<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>分类: 技术教程 | 小小程序员</title><meta name="author" content="十一星野"><meta name="copyright" content="十一星野"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="如果你看了 《Kubernetes太火了！花10分钟玩转它不香么？》一文的话，基本上已经可以玩转 K8S 了。其实 K8S 中还有一些高级特性也很值得学习，比如弹性扩缩应用、滚动更新、配置管理、存储卷、网关路由等。今天我们就来了解下这些高级特性，希望对大家有所帮助！  #核心概念 首先我们先来了解一些核心概念，了解这些核心概念对使用K8S的高级特性很有帮助。  #ReplicaSetReplic">
<meta property="og:type" content="article">
<meta property="og:title" content="自从上了K8S，项目更新都不带停机的！">
<meta property="og:url" content="https://ko25891wan.gitlab.io/2024/01/9bd6f132d081.html">
<meta property="og:site_name" content="小小程序员">
<meta property="og:description" content="如果你看了 《Kubernetes太火了！花10分钟玩转它不香么？》一文的话，基本上已经可以玩转 K8S 了。其实 K8S 中还有一些高级特性也很值得学习，比如弹性扩缩应用、滚动更新、配置管理、存储卷、网关路由等。今天我们就来了解下这些高级特性，希望对大家有所帮助！  #核心概念 首先我们先来了解一些核心概念，了解这些核心概念对使用K8S的高级特性很有帮助。  #ReplicaSetReplic">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png">
<meta property="article:published_time" content="2024-01-21T09:20:07.000Z">
<meta property="article:modified_time" content="2024-02-03T02:44:57.267Z">
<meta property="article:author" content="十一星野">
<meta property="article:tag" content="宅男,热血">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://ko25891wan.gitlab.io/2024/01/9bd6f132d081.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.staticfile.org/egjs-infinitegrid/4.11.0/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '分类: 技术教程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-03 10:44:57'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="小小程序员"><span class="site-name">小小程序员</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">自从上了K8S，项目更新都不带停机的！</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-01-21T09:20:07.000Z" title="发表于 2024-01-21 17:20:07">2024-01-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-03T02:44:57.267Z" title="更新于 2024-02-03 10:44:57">2024-02-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/">技术教程</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/">mall</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/k8s/">k8s</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="自从上了K8S，项目更新都不带停机的！"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><blockquote>
<p>如果你看了 <a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_start.html">《Kubernetes太火了！花10分钟玩转它不香么？》</a>一文的话，基本上已经可以玩转 K8S 了。其实 K8S 中还有一些高级特性也很值得学习，比如弹性扩缩应用、滚动更新、配置管理、存储卷、网关路由等。今天我们就来了解下这些高级特性，希望对大家有所帮助！</p>
</blockquote>
<h2 id="核心概念"><a href="#核心概念" class="headerlink" title="#核心概念"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5">#</a>核心概念</h2><blockquote>
<p>首先我们先来了解一些核心概念，了解这些核心概念对使用K8S的高级特性很有帮助。</p>
</blockquote>
<h3 id="ReplicaSet"><a href="#ReplicaSet" class="headerlink" title="#ReplicaSet"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#replicaset">#</a>ReplicaSet</h3><p>ReplicaSet确保任何时间都有指定数量的Pod副本在运行。通常用来保证给定数量的、完全相同的Pod的可用性。建议使用Deployment来管理ReplicaSet，而不是直接使用ReplicaSet。</p>
<h3 id="ConfigMap"><a href="#ConfigMap" class="headerlink" title="#ConfigMap"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#configmap">#</a>ConfigMap</h3><p>ConfigMap是一种API对象，用来将非机密性的数据保存到键值对中。使用时，Pod可以将其用作环境变量、命令行参数或者存储卷中的配置文件。使用ConfigMap可以将你的配置数据和应用程序代码分开。</p>
<h3 id="Volume"><a href="#Volume" class="headerlink" title="#Volume"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#volume">#</a>Volume</h3><p>Volume指的是存储卷，包含可被Pod中容器访问的数据目录。容器中的文件在磁盘上是临时存放的，当容器崩溃时文件会丢失，同时无法在多个Pod中共享文件，通过使用存储卷可以解决这两个问题。</p>
<p>常用的存储卷有如下几种：</p>
<ul>
<li>configMap：configMap卷提供了向Pod注入配置数据的方法。ConfigMap对象中存储的数据可以被configMap类型的卷引用，然后被Pod中运行的容器化应用使用。</li>
<li>emptyDir：emptyDir卷可用于存储缓存数据。当Pod分派到某个Node上时，emptyDir卷会被创建，并且Pod在该节点上运行期间，卷一直存在。当Pod被从节点上删除时emptyDir卷中的数据也会被永久删除。</li>
<li>hostPath：hostPath卷能将主机节点文件系统上的文件或目录挂载到你的Pod中。在Minikube中的主机指的是Minikube所在虚拟机。</li>
<li>local：local卷所代表的是某个被挂载的本地存储设备，例如磁盘、分区或者目录。local卷只能用作静态创建的持久卷，尚不支持动态配置。</li>
<li>nfs：nfs卷能将NFS（网络文件系统）挂载到你的Pod中。</li>
<li>persistentVolumeClaim：persistentVolumeClaim卷用来将持久卷（PersistentVolume）挂载到Pod中。持久卷（PV）是集群中的一块存储，可以由管理员事先供应，或者使用存储类（Storage Class）来动态供应，持久卷是集群资源类似于节点。</li>
</ul>
<h3 id="Ingress"><a href="#Ingress" class="headerlink" title="#Ingress"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#ingress">#</a>Ingress</h3><p>Ingress类似于K8S中的网关服务，是对集群中服务的外部访问进行管理的API对象，典型的访问方式是HTTP。Ingress可以提供负载均衡、SSL终结和基于名称的虚拟托管。</p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-03_10:44:54_31f44d9123bc2022088e2b5943d83204.png"></p>
<h2 id="高级特性"><a href="#高级特性" class="headerlink" title="#高级特性"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#%E9%AB%98%E7%BA%A7%E7%89%B9%E6%80%A7">#</a>高级特性</h2><h3 id="扩缩应用"><a href="#扩缩应用" class="headerlink" title="#扩缩应用"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#%E6%89%A9%E7%BC%A9%E5%BA%94%E7%94%A8">#</a>扩缩应用</h3><blockquote>
<p>当流量增加时，我们需要扩容应用程序满足用户需求。当流量减少时，需要缩放应用以减少服务器开销。在K8S中扩缩是通过改变Deployment中的副本数量来实现的。</p>
</blockquote>
<ul>
<li>获取所有Deployment可使用如下命令：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get deployments</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NAME               READY   UP-TO-DATE   AVAILABLE   AGE</span><br><span class="line">kubernetes-nginx   1/1     1            1           43h</span><br></pre></td></tr></table></figure>

<ul>
<li>获取所有ReplicaSet可使用如下命令：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get rs</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NAME                          DESIRED   CURRENT   READY   AGE</span><br><span class="line">kubernetes-nginx-78bcc44665   1         1         1       43h</span><br></pre></td></tr></table></figure>

<ul>
<li>对应用进行扩容操作，扩容到4个实例，再查看所有：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl scale deployments/kubernetes-nginx --replicas=4</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[macro@linux-local root]$ kubectl get deployments</span><br><span class="line">NAME               READY   UP-TO-DATE   AVAILABLE   AGE</span><br><span class="line">kubernetes-nginx   4/4     4            4           43h</span><br></pre></td></tr></table></figure>

<ul>
<li>查看所有Pod，发现已经有4个运行在不同的IP地址上了；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pods -o wide</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">NAME                                READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES</span><br><span class="line">kubernetes-nginx-78bcc44665-8fnnn   1/1     Running   2          43h   172.17.0.3   minikube   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kubernetes-nginx-78bcc44665-dvq4t   1/1     Running   0          84s   172.17.0.8   minikube   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kubernetes-nginx-78bcc44665-thzg9   1/1     Running   0          84s   172.17.0.7   minikube   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kubernetes-nginx-78bcc44665-w7xqd   1/1     Running   0          84s   172.17.0.6   minikube   &lt;none&gt;           &lt;none&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>对应用进行缩放操作，缩放到2个实例；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl scale deployments/kubernetes-nginx --replicas=2</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">NAME                                READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES</span><br><span class="line">kubernetes-nginx-78bcc44665-8fnnn   1/1     Running   2          44h   172.17.0.3   minikube   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kubernetes-nginx-78bcc44665-w7xqd   1/1     Running   0          11m   172.17.0.6   minikube   &lt;none&gt;           &lt;none&gt;</span><br></pre></td></tr></table></figure>

<h3 id="滚动更新"><a href="#滚动更新" class="headerlink" title="#滚动更新"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#%E6%BB%9A%E5%8A%A8%E6%9B%B4%E6%96%B0">#</a>滚动更新</h3><blockquote>
<p>滚动更新允许通过使用新的实例逐步更新Pod实例，零停机进行Deployment更新。K8S不仅可以实现滚动更新，还可以支持回滚操作。</p>
</blockquote>
<ul>
<li>目前运行了4个Nginx<code>1.10</code>版本的实例：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[macro@linux-local root]$ kubectl get pods</span><br><span class="line">NAME                                READY   STATUS    RESTARTS   AGE</span><br><span class="line">kubernetes-nginx-78bcc44665-8fnnn   1/1     Running   2          44h</span><br><span class="line">kubernetes-nginx-78bcc44665-jpw2g   1/1     Running   0          5s</span><br><span class="line">kubernetes-nginx-78bcc44665-w7xqd   1/1     Running   0          59m</span><br><span class="line">kubernetes-nginx-78bcc44665-xx8s5   1/1     Running   0          5s</span><br></pre></td></tr></table></figure>

<ul>
<li>可以通过<code>kubectl describe</code>命令来查看镜像版本号：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[macro@linux-local root]$ kubectl describe pods |grep Image</span><br><span class="line">    Image:          nginx:1.10</span><br><span class="line">    Image ID:       docker-pullable://nginx@sha256:6202beb06ea61f44179e02ca965e8e13b961d12640101fca213efbfd145d7575</span><br></pre></td></tr></table></figure>

<ul>
<li>通过<code>kubectl set image</code>命令来更新Nginx镜像的版本号为<code>1.19</code>，此时K8S会执行滚动更新，逐步停止<code>1.10</code>版本的实例并启动<code>1.19</code>版本的实例；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 命令格式 kubectl set image Deployment的名称 容器名称=容器镜像:镜像版本号</span><br><span class="line">kubectl set image deployments/kubernetes-nginx nginx=nginx:1.19</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"># 停止1个旧实例并创建2个新实例</span><br><span class="line">NAME                                READY   STATUS              RESTARTS   AGE</span><br><span class="line">kubernetes-nginx-66f67cd758-rbcz5   0/1     ContainerCreating   0          11s</span><br><span class="line">kubernetes-nginx-66f67cd758-s9ck8   0/1     ContainerCreating   0          11s</span><br><span class="line">kubernetes-nginx-78bcc44665-8fnnn   1/1     Running             2          45h</span><br><span class="line">kubernetes-nginx-78bcc44665-jpw2g   0/1     Terminating         0          15m</span><br><span class="line">kubernetes-nginx-78bcc44665-w7xqd   1/1     Running             0          75m</span><br><span class="line">kubernetes-nginx-78bcc44665-xx8s5   1/1     Running             0          15m</span><br><span class="line"># 1个实例已被停止2个新实例仍创建中</span><br><span class="line">NAME                                READY   STATUS              RESTARTS   AGE</span><br><span class="line">kubernetes-nginx-66f67cd758-rbcz5   0/1     ContainerCreating   0          30s</span><br><span class="line">kubernetes-nginx-66f67cd758-s9ck8   0/1     ContainerCreating   0          30s</span><br><span class="line">kubernetes-nginx-78bcc44665-8fnnn   1/1     Running             2          45h</span><br><span class="line">kubernetes-nginx-78bcc44665-w7xqd   1/1     Running             0          75m</span><br><span class="line">kubernetes-nginx-78bcc44665-xx8s5   1/1     Running             0          15m</span><br><span class="line"># 4个新实例均已创建完成</span><br><span class="line">NAME                                READY   STATUS    RESTARTS   AGE</span><br><span class="line">kubernetes-nginx-66f67cd758-jn926   1/1     Running   0          48s</span><br><span class="line">kubernetes-nginx-66f67cd758-rbcz5   1/1     Running   0          3m12s</span><br><span class="line">kubernetes-nginx-66f67cd758-s9ck8   1/1     Running   0          3m12s</span><br><span class="line">kubernetes-nginx-66f67cd758-smr7n   1/1     Running   0          44s</span><br></pre></td></tr></table></figure>

<ul>
<li>此时再使用<code>kubectl describe</code>命令来查看镜像版本号，发现Nginx已经更新至<code>1.19</code>版本：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[macro@linux-local root]$ kubectl describe pods |grep Image</span><br><span class="line">    Image:          nginx:1.19</span><br><span class="line">    Image ID:       docker-pullable://nginx@sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9</span><br></pre></td></tr></table></figure>

<ul>
<li>如果想回滚到原来的版本的话，直接使用<code>kubectl rollout undo</code>命令即可。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl rollout undo deployments/kubernetes-nginx</span><br></pre></td></tr></table></figure>

<h3 id="配置管理"><a href="#配置管理" class="headerlink" title="#配置管理"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86">#</a>配置管理</h3><blockquote>
<p>ConfigMap允许你将配置文件与镜像文件分离，以使容器化的应用程序具有可移植性。接下来我们演示下如何将ConfigMap的的属性注入到Pod的环境变量中去。</p>
</blockquote>
<ul>
<li>添加配置文件<code>nginx-config.yaml</code>用于创建ConfigMap，ConfigMap名称为<code>nginx-config</code>，配置信息存放在<code>data</code>节点下：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">apiVersion: v1</span><br><span class="line">kind: ConfigMap</span><br><span class="line">metadata:</span><br><span class="line">  name: nginx-config</span><br><span class="line">  namespace: default</span><br><span class="line">data:</span><br><span class="line">  nginx-env: test</span><br></pre></td></tr></table></figure>

<ul>
<li>应用<code>nginx-config.yaml</code>文件创建ConfigMap：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl create -f nginx-config.yaml</span><br></pre></td></tr></table></figure>

<ul>
<li>获取所有ConfigMap：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get configmap</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">NAME               DATA   AGE</span><br><span class="line">kube-root-ca.crt   1      2d22h</span><br><span class="line">nginx-config       1      13s</span><br></pre></td></tr></table></figure>

<ul>
<li>通过<code>yaml</code>格式查看ConfigMap中的内容：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get configmaps nginx-config -o yaml</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">apiVersion: v1</span><br><span class="line">data:</span><br><span class="line">  nginx-env: test</span><br><span class="line">kind: ConfigMap</span><br><span class="line">metadata:</span><br><span class="line">  creationTimestamp: &quot;2021-01-08T01:49:44Z&quot;</span><br><span class="line">  managedFields:</span><br><span class="line">  - apiVersion: v1</span><br><span class="line">    fieldsType: FieldsV1</span><br><span class="line">    fieldsV1:</span><br><span class="line">      f:data:</span><br><span class="line">        .: &#123;&#125;</span><br><span class="line">        f:nginx-env: &#123;&#125;</span><br><span class="line">    manager: kubectl-create</span><br><span class="line">    operation: Update</span><br><span class="line">    time: &quot;2021-01-08T01:49:44Z&quot;</span><br><span class="line">  name: nginx-config</span><br><span class="line">  namespace: default</span><br><span class="line">  resourceVersion: &quot;61322&quot;</span><br><span class="line">  uid: a477567f-2aff-4a04-9a49-f19220baf0d3</span><br></pre></td></tr></table></figure>

<ul>
<li>添加配置文件<code>nginx-deployment.yaml</code>用于创建Deployment，部署一个Nginx服务，在Nginx的环境变量中引用ConfigMap中的属性：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">apiVersion: apps/v1</span><br><span class="line">kind: Deployment</span><br><span class="line">metadata:</span><br><span class="line">  name: nginx-deployment</span><br><span class="line">  labels:</span><br><span class="line">    app: nginx</span><br><span class="line">spec:</span><br><span class="line">  replicas: 1</span><br><span class="line">  selector:</span><br><span class="line">    matchLabels:</span><br><span class="line">      app: nginx</span><br><span class="line">  template:</span><br><span class="line">    metadata:</span><br><span class="line">      labels:</span><br><span class="line">        app: nginx</span><br><span class="line">    spec:</span><br><span class="line">      containers:</span><br><span class="line">        - name: nginx</span><br><span class="line">          image: nginx:1.10</span><br><span class="line">          ports:</span><br><span class="line">            - containerPort: 80</span><br><span class="line">          env:</span><br><span class="line">            - name: NGINX_ENV # 在Nginx中设置环境变量</span><br><span class="line">              valueFrom:</span><br><span class="line">                configMapKeyRef:</span><br><span class="line">                  name: nginx-config # 设置ConfigMap的名称</span><br><span class="line">                  key: nginx-env # 需要取值的键      </span><br></pre></td></tr></table></figure>

<ul>
<li>应用配置文件文件创建Deployment：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl apply -f nginx-deployment.yaml</span><br></pre></td></tr></table></figure>

<ul>
<li>创建成功后查看Pod中的环境变量，发现<code>NGINX_ENV</code>变量已经被注入了；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl exec deployments/nginx-deployment -- env</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin</span><br><span class="line">HOSTNAME=nginx-deployment-66fcf997c-xxdsb</span><br><span class="line">NGINX_ENV=test</span><br></pre></td></tr></table></figure>

<h3 id="存储卷使用"><a href="#存储卷使用" class="headerlink" title="#存储卷使用"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#%E5%AD%98%E5%82%A8%E5%8D%B7%E4%BD%BF%E7%94%A8">#</a>存储卷使用</h3><blockquote>
<p>通过存储卷，我们可以把外部数据挂载到容器中去，供容器中的应用访问，这样就算容器崩溃了，数据依然可以存在。</p>
</blockquote>
<ul>
<li>记得之前我们使用Docker部署Nginx的时候，将Nginx的<code>html、logs、conf</code>目录从外部挂载到了容器中；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker run -p 80:80 --name nginx \</span><br><span class="line">-v /mydata/nginx/html:/usr/share/nginx/html \</span><br><span class="line">-v /mydata/nginx/logs:/var/log/nginx  \</span><br><span class="line">-v /mydata/nginx/conf:/etc/nginx \</span><br><span class="line">-d nginx:1.10</span><br></pre></td></tr></table></figure>

<ul>
<li>Minikube可以认为是一台虚拟机，我们可以用Minikube的<code>ssh</code>命令来访问它；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">minikube ssh</span><br></pre></td></tr></table></figure>

<ul>
<li>Minikube中默认有一个<code>docker</code>用户，我们先重置下它的密码；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo passwd docker</span><br></pre></td></tr></table></figure>

<ul>
<li>在Minikube中创建<code>mydata</code>目录；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">midir /home/docker/mydata</span><br></pre></td></tr></table></figure>

<ul>
<li>我们需要把Nginx的数据目录复制到Minikube中去，才能实现目录的挂载，注意docker用户只能修改<code>/home/docker</code>目录中的文件，我们通过<code>scp</code>命令来复制文件；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scp -r /home/macro/mydata/nginx docker@192.168.49.2:/home/docker/mydata/nginx</span><br></pre></td></tr></table></figure>

<ul>
<li>添加配置文件<code>nginx-volume-deployment.yaml</code>用于创建Deployment：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">apiVersion: apps/v1</span><br><span class="line">kind: Deployment</span><br><span class="line">metadata:</span><br><span class="line">  name: nginx-volume-deployment</span><br><span class="line">  labels:</span><br><span class="line">    app: nginx</span><br><span class="line">spec:</span><br><span class="line">  replicas: 1</span><br><span class="line">  selector:</span><br><span class="line">    matchLabels:</span><br><span class="line">      app: nginx</span><br><span class="line">  template:</span><br><span class="line">    metadata:</span><br><span class="line">      labels:</span><br><span class="line">        app: nginx</span><br><span class="line">    spec:</span><br><span class="line">      containers:</span><br><span class="line">        - name: nginx</span><br><span class="line">          image: nginx:1.10</span><br><span class="line">          ports:</span><br><span class="line">            - containerPort: 80</span><br><span class="line">          volumeMounts:</span><br><span class="line">            - mountPath: /usr/share/nginx/html</span><br><span class="line">              name: html-volume</span><br><span class="line">            - mountPath: /var/log/nginx</span><br><span class="line">              name: logs-volume</span><br><span class="line">            - mountPath: /etc/nginx</span><br><span class="line">              name: conf-volume</span><br><span class="line">      volumes:</span><br><span class="line">        - name: html-volume</span><br><span class="line">          hostPath:</span><br><span class="line">            path: /home/docker/mydata/nginx/html</span><br><span class="line">            type: Directory</span><br><span class="line">        - name: logs-volume</span><br><span class="line">          hostPath:</span><br><span class="line">            path: /home/docker/mydata/nginx/logs</span><br><span class="line">            type: Directory</span><br><span class="line">        - name: conf-volume</span><br><span class="line">          hostPath:</span><br><span class="line">            path: /home/docker/mydata/nginx/conf</span><br><span class="line">            type: Directory</span><br></pre></td></tr></table></figure>

<ul>
<li>应用配置文件创建Deployment；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl apply -f nginx-volume-deployment.yaml</span><br></pre></td></tr></table></figure>

<ul>
<li>添加配置文件<code>nginx-service.yaml</code>用于创建Service；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">apiVersion: v1</span><br><span class="line">kind: Service</span><br><span class="line">metadata:</span><br><span class="line">  name: nginx-service</span><br><span class="line">spec:</span><br><span class="line">  type: NodePort</span><br><span class="line">  selector:</span><br><span class="line">    app: nginx</span><br><span class="line">  ports:</span><br><span class="line">    - name: http</span><br><span class="line">      protocol: TCP</span><br><span class="line">      port: 80</span><br><span class="line">      targetPort: 80</span><br><span class="line">      nodePort: 30080</span><br></pre></td></tr></table></figure>

<ul>
<li>应用配置文件创建Service；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl apply -f nginx-service.yaml</span><br></pre></td></tr></table></figure>

<ul>
<li>查看下Service服务访问端口；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[macro@linux-local nginx]$ kubectl get services</span><br><span class="line">NAME                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE</span><br><span class="line">kubernetes                ClusterIP   10.96.0.1       &lt;none&gt;        443/TCP        6d23h</span><br><span class="line">kubernetes-nginx          NodePort    10.106.227.54   &lt;none&gt;        80:30158/TCP   5d22h</span><br><span class="line">nginx-service             NodePort    10.103.72.111   &lt;none&gt;        80:30080/TCP   7s</span><br></pre></td></tr></table></figure>

<ul>
<li>通过CURL命令可以访问Nginx首页信息。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl $(minikube ip):30080</span><br></pre></td></tr></table></figure>

<h3 id="网关路由"><a href="#网关路由" class="headerlink" title="#网关路由"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#%E7%BD%91%E5%85%B3%E8%B7%AF%E7%94%B1">#</a>网关路由</h3><blockquote>
<p>Ingress可以作为K8S的网关来使用，能提供服务路由和负载均衡等功能。</p>
</blockquote>
<ul>
<li>Minikube默认没有启用Ingress插件，需要手动开启；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">minikube addons enable ingress</span><br></pre></td></tr></table></figure>

<ul>
<li>开启Ingress过程中遇到了一个坑，会在验证的时候卡主，其实是Minikube内部无法下载Ingress镜像导致的：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[macro@linux-local ~]$ minikube addons enable ingress</span><br><span class="line">* Verifying ingress addon...</span><br></pre></td></tr></table></figure>

<ul>
<li>解决该问题需要手动下载第三方镜像，并标记为需要的镜像，并重新启用Ingress插件；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># 查找启动有问题的Pod</span><br><span class="line">kubectl get pods -n kube-system</span><br><span class="line"># 查看启动失败原因</span><br><span class="line">kubectl describe ingress-nginx-controller-xxx -n kube-system</span><br><span class="line"># 连接到Minikube</span><br><span class="line">minikube ssh</span><br><span class="line"># 原来需要下载的镜像（已经无法下载）</span><br><span class="line">docker pull us.gcr.io/k8s-artifacts-prod/ingress-nginx/controller:v0.40.2</span><br><span class="line"># 下载第三方替代镜像（直接去DockerHub官网搜索即可）</span><br><span class="line">docker pull pollyduan/ingress-nginx-controller:v0.40.2</span><br><span class="line"># 修改镜像名称</span><br><span class="line">docker tag pollyduan/ingress-nginx-controller:v0.40.2 us.gcr.io/k8s-artifacts-prod/ingress-nginx/controller:v0.40.2 </span><br></pre></td></tr></table></figure>

<ul>
<li>重启插件后检查下Ingress是否在运行；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pods -n kube-system</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">NAME                                        READY   STATUS      RESTARTS   AGE</span><br><span class="line">ingress-nginx-admission-create-krpgk        0/1     Completed   0          46h</span><br><span class="line">ingress-nginx-admission-patch-wnxlk         0/1     Completed   3          46h</span><br><span class="line">ingress-nginx-controller-558664778f-wwgws   1/1     Running     2          46h</span><br></pre></td></tr></table></figure>

<ul>
<li>添加配置文件<code>nginx-ingress.yaml</code>用于创建Ingress；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">apiVersion: networking.k8s.io/v1</span><br><span class="line">kind: Ingress</span><br><span class="line">metadata:</span><br><span class="line">  name: nginx-ingress</span><br><span class="line">  annotations:</span><br><span class="line">    nginx.ingress.kubernetes.io/rewrite-target: /$1</span><br><span class="line">spec:</span><br><span class="line">  rules:</span><br><span class="line">    - host: nginx-volume.com</span><br><span class="line">      http:</span><br><span class="line">        paths:</span><br><span class="line">          - path: /</span><br><span class="line">            pathType: Prefix</span><br><span class="line">            backend:</span><br><span class="line">              service:</span><br><span class="line">                name: nginx-service</span><br><span class="line">                port:</span><br><span class="line">                  number: 80</span><br></pre></td></tr></table></figure>

<ul>
<li>应用配置文件创建Ingress；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl apply -f nginx-ingress.yaml</span><br></pre></td></tr></table></figure>

<ul>
<li>查看所有Ingress，此时我们已经可以通过<code>nginx-volume.com</code>来访问Pod中运行的Nginx服务了；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get ingress</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NAME            CLASS    HOSTS              ADDRESS        PORTS   AGE</span><br><span class="line">nginx-ingress   &lt;none&gt;   nginx-volume.com   192.168.49.2   80      6s</span><br></pre></td></tr></table></figure>

<ul>
<li>需要修改下<code>host</code>文件，注意切换到<code>root</code>账号后修改：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 切换到root用户</span><br><span class="line">su -</span><br><span class="line"># 修改host文件</span><br><span class="line">vi /etc/hosts</span><br><span class="line"># 添加如下记录</span><br><span class="line">192.168.49.2 nginx-volume.com</span><br></pre></td></tr></table></figure>

<ul>
<li>最后通过CURL命令可以访问Nginx首页信息。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl nginx-volume.com</span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="#总结"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#%E6%80%BB%E7%BB%93">#</a>总结</h2><p>通过K8S扩展和管理容器化应用确实十分方便，通过几个命令我们就可以实现零停机更新，出了故障也不怕，一个命令实现回滚。但是大量的命令行操作总显得枯燥无味，要是有个可视化工具可以直接管理K8S就更好了。</p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="#参考资料"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/k8s/k8s_advance_start.html#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99">#</a>参考资料</h2><p>官方文档：<a target="_blank" rel="noopener" href="https://kubernetes.io/zh/docs/home/">https://kubernetes.io/zh/docs/home/</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io">十一星野</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io/2024/01/9bd6f132d081.html">https://ko25891wan.gitlab.io/2024/01/9bd6f132d081.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ko25891wan.gitlab.io" target="_blank">小小程序员</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2024/01/ea4b93891755.html" title="再见命令行！K8S傻瓜式安装，图形化管理真香！"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">再见命令行！K8S傻瓜式安装，图形化管理真香！</div></div></a></div><div class="next-post pull-right"><a href="/2024/01/4b8ac310efe9.html" title="K8S太火了！花10分钟玩转它不香么？"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">K8S太火了！花10分钟玩转它不香么？</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">十一星野</div><div class="author-info__description">归途也还可爱</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Gitlab"><i class="fab fa-gitlab" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:ko25891wan@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5"><span class="toc-number">1.</span> <span class="toc-text">核心概念</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#ReplicaSet"><span class="toc-number">1.1.</span> <span class="toc-text">ReplicaSet</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ConfigMap"><span class="toc-number">1.2.</span> <span class="toc-text">ConfigMap</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Volume"><span class="toc-number">1.3.</span> <span class="toc-text">Volume</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Ingress"><span class="toc-number">1.4.</span> <span class="toc-text">Ingress</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%AB%98%E7%BA%A7%E7%89%B9%E6%80%A7"><span class="toc-number">2.</span> <span class="toc-text">高级特性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%A9%E7%BC%A9%E5%BA%94%E7%94%A8"><span class="toc-number">2.1.</span> <span class="toc-text">扩缩应用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%BB%9A%E5%8A%A8%E6%9B%B4%E6%96%B0"><span class="toc-number">2.2.</span> <span class="toc-text">滚动更新</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86"><span class="toc-number">2.3.</span> <span class="toc-text">配置管理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%98%E5%82%A8%E5%8D%B7%E4%BD%BF%E7%94%A8"><span class="toc-number">2.4.</span> <span class="toc-text">存储卷使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E5%85%B3%E8%B7%AF%E7%94%B1"><span class="toc-number">2.5.</span> <span class="toc-text">网关路由</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">3.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">4.</span> <span class="toc-text">参考资料</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/c60470b9a892.html" title="Java集合使用注意事项总结">Java集合使用注意事项总结</a><time datetime="2024-02-03T04:58:22.000Z" title="发表于 2024-02-03 12:58:22">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/81fb7a201f29.html" title="无题">无题</a><time datetime="2024-02-03T03:15:31.812Z" title="发表于 2024-02-03 11:15:31">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/5737c1ec69a9.html" title="Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)">Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)</a><time datetime="2024-01-31T06:56:16.000Z" title="发表于 2024-01-31 14:56:16">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/f510db7d2f19.html" title="Spring常见面试题总结">Spring常见面试题总结</a><time datetime="2024-01-31T06:55:58.000Z" title="发表于 2024-01-31 14:55:58">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/9be065e9f288.html" title="redis 面试">redis 面试</a><time datetime="2024-01-31T06:55:55.000Z" title="发表于 2024-01-31 14:55:55">2024-01-31</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By 十一星野</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.staticfile.org/instant.page/5.2.0/instantpage.min.js" type="module"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>